package com.dayuanit.dy15.atm.controller;

import com.dayuanit.dy15.atm.dto.FlowDTO;
import com.dayuanit.dy15.atm.dto.ResponseDTO;
import com.dayuanit.dy15.atm.holder.PageHolder;
import com.dayuanit.dy15.atm.service.FlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.List;

@RestController
public class FlowController extends BaseController {

    public FlowController() {
        System.out.println("FlowController");
    }

    @Autowired
    private FlowService flowService;

    public void setFlowService(FlowService flowService) {
        this.flowService = flowService;
    }

    @RequestMapping("/flow/listFlow.do")
    public ResponseDTO listFlow(HttpServletRequest request, HttpServletResponse response) {
        String cardId = request.getParameter("cardId");
        String currentPage = request.getParameter("currentPage");

        PageHolder<List<FlowDTO>> pageHolder = flowService.queryFLows(Long.valueOf(cardId), Integer.parseInt(currentPage));

        return ResponseDTO.sucess(pageHolder);
    }


    @RequestMapping("/card/listTop10.do")
    public ResponseDTO listTop10(HttpServletRequest request, HttpServletResponse response) {
        return ResponseDTO.sucess(flowService.top10(getUserId(request)));
    }

    @RequestMapping("/flow/downLoadFlow.do")
    public void downLoadFlow(long cardId, HttpServletRequest request, HttpServletResponse response) {
        List<FlowDTO> flowDTOS = flowService.listFlow4DownLoad(getUserId(request), cardId);

        String fileName = "flow.csv";
        if (!flowDTOS.isEmpty()) {
           fileName = flowDTOS.get(0).getCardNum() + ".csv";
        }

        //告诉浏览器 我返回的内容是二进制的流信息，这样浏览器就会进行下载动作，而不是打开
        response.setContentType("application/octet-stream");
        //此响应头，告诉浏览器，下载的文件名是多少
        response.addHeader("Content-Disposition", "attachment; filename=" + fileName);

        StringBuilder sx = new StringBuilder();

        //创建表头
        sx.append("卡号").append(",")
                .append("金额").append(",")
                .append("备注").append(",")
                .append("时间").append(",").append("\n");

        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));
            //流水内容
            for (FlowDTO flowDTO : flowDTOS) {
                sx.append(flowDTO.getCardNum()).append(",")
                        .append(flowDTO.getAmount()).append(",")
                        .append(flowDTO.getRemark()).append(",")
                        .append(flowDTO.getCreateTime()).append("\n");
            }
            bw.write(sx.toString());
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
